package tests;

import java.util.List;

import org.springframework.orm.jpa.support.JpaDaoSupport;

public class UserDao extends JpaDaoSupport implements IUserDao {

	public void delete(IUser entity) {
		System.out.println("Delete User");
		try {
			entity = getJpaTemplate().getReference(User.class, entity.getId());
			getJpaTemplate().remove(entity);
		} catch (RuntimeException e) {
			throw e;
		}

	}

	public IUser update(User entity) {
		System.out.println("Update User");
		try {
			IUser resault = getJpaTemplate().merge(entity);
			return resault;
		} catch (RuntimeException e) {
			throw e;
		}

	}

	public IUser findById(long id) {
		System.out.println("find by id");
		try {
			IUser instance = getJpaTemplate().find(User.class, id);
			// System.out.println("find by id instance"+instance);
			return instance;
		} catch (RuntimeException e) {
			throw e;
		}

	}

	public List<User> findAll() {
		System.out.println("find all ");
		try {
			return getJpaTemplate().find("from User");
		} catch (RuntimeException e) {
			throw e;
		}

	}

	public void save(IUser entity) {
		System.out.println("Saving User");
		try {
			getJpaTemplate().persist(entity);
		} catch (RuntimeException e) {
			throw e;
		}

	}

}
